package com.video.main_video.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.upload.upload_module.manager.handler.upload.DownloadManager;
import com.upload.upload_module.manager.handler.upload.UploadManager;
import com.upload.upload_module.manager.request.UploadRequest;
import com.upload.upload_module.manager.response.UploadResponse;
import com.video.main_video.common.jwt.JwtUtils;
import com.video.main_video.common.request.BaseRequest;
import com.video.main_video.common.utils.ImgUtils;
import com.video.main_video.common.utils.VideoUtils;
import com.video.main_video.dao.entity.VideoInfo;
import com.video.main_video.dao.request.userInfo.UserVideoRequest;
import com.video.main_video.manager.handle.uploadFile.UploadFile;
import com.video.main_video.manager.handle.uploadFile.VideoGet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@RestController
@RequestMapping("/")
public class UploadController {

    @Autowired
    UploadFile uploadFile;

    UploadManager uploadManager =new UploadManager();
    DownloadManager downloadManager=new DownloadManager();
    @PostMapping("upload")
    private UploadResponse upload(@RequestParam("file") MultipartFile file, HttpServletRequest  httpReq) throws IOException {
        UploadRequest req=new UploadRequest();
        for (Cookie cookie : httpReq.getCookies()) {
            System.out.println(cookie.getValue());
        }
        req.setFile(file);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        LocalDate date = LocalDate.now(); // get the current date
        String token = httpReq.getHeader("Authorization");
        DecodedJWT verify = JwtUtils.verify(token);

        String userId = verify.getClaim("userId").asString();

        String path=userId+"\\"+date.format(formatter);
        String filePath = ImgUtils.generaPath(path);


        req.setPath(filePath+"\\");

        UploadResponse res=uploadManager.handler(req);

        System.out.println("上传文件");
        System.out.println(file.getOriginalFilename());
        VideoInfo videoInfo=new VideoInfo();
        videoInfo.setVideoDir(path);
        videoInfo.setVideoName(file.getOriginalFilename());
        videoInfo.setCutId(UUID.randomUUID().toString().replace("-",""));

        res.setCode(200);

        System.out.println(token);

        videoInfo.setUserId(userId);
//       保存到数据库
        uploadFile.handler(videoInfo);

        res.setFileId(videoInfo.getCutId());
        return res;
    }

    @GetMapping("download")
    private void downloadFile(@RequestParam("fileName") String fileName,HttpServletRequest request, HttpServletResponse response){
        System.out.println("请求下载");
        String path="G:\\JavaProject\\cut_video\\src\\main\\resources\\video\\wdx\\"+fileName;
        System.out.println(path);
        downloadManager.handler(path,request,response);
    }

    @GetMapping("getVideo")
    private void getVideo(@RequestParam("video") String videoSrc,HttpServletRequest request, HttpServletResponse response){

        VideoGet video=new VideoGet();
        video.handler(videoSrc,request, response);
    }
}
